﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
using System.Windows.Markup;
using SmartPaginator.Extensions;

namespace SmartPaginator.Elements
{
    public class PageColumnElement : BlockContainerElement
    {
        #region Constructor

        protected PageColumnElement(): base(null)
        {
        }

        public PageColumnElement(FlowDocument document, Size columnSize, IElementFactory factory)
            : base(null, document.Blocks, factory)
        {
            Document = document;

            this.FontFamily = document.FontFamily;
            this.FontSize = document.FontSize;
            this.FontStretch = document.FontStretch;
            this.FontStyle = document.FontStyle;
            this.FontWeight = document.FontWeight;

            this.Width = columnSize.Width;
            this.MinHeight = columnSize.Height;
        }

        #endregion

        #region Properties

        private FlowDocument Document { get; set; }

        public SectionElement Section
        {
            get
            {
                if (this.Items.Count == 0)
                    return null;

                return Items[0] as SectionElement;
            }
        }

        #endregion

        #region Render Methods

        public void Render(bool renderAsSinglePage)
        {
            double height = this.MinHeight;
            if (renderAsSinglePage)
                height = double.PositiveInfinity;

            Size pageSize = new Size(this.Width, height);
            this.Measure(pageSize);

            pageSize.Height = Math.Max(this.DesiredSize.Height, this.MinHeight);
            Rect pageBounds = new Rect(pageSize);
            this.Arrange(pageBounds);
        }

        #endregion

        #region GetEmptyCopy Method

        public override IBlockElement GetEmptyCopy()
        {
            var result = new PageColumnElement();
            result.Width = this.Width;
            result.MinHeight = this.MinHeight;

            result.FontFamily = this.FontFamily;
            result.FontSize = this.FontSize;
            result.FontStretch = this.FontStretch;
            result.FontStyle = this.FontStyle;
            result.FontWeight = this.FontWeight;

            return result;
        }

        #endregion

        #region IsDirectChild Method

        public static bool IsDirectChild(FrameworkElement element)
        {
            if (element == null)
                return false;

            var parent = element.Parent;
            if (parent is PageColumnElement)
                return true;
            if (parent is SectionElement)
                return IsDirectChild(parent as FrameworkElement);

            return false;
        }

        #endregion
    }
}
